<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="规则名称" prop="ruleName">
						<el-input v-model="form.ruleName" placeholder="请输入规则名称" :disabled="!getEditable()" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="场景标识" prop="sceneKey">
						<el-input v-model="form.sceneKey" placeholder="请输入场景标识" :disabled="!getEditable()" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="规则类型" prop="ruleType">
						<el-input v-model="form.ruleType" placeholder="请输入规则类型" :disabled="!getEditable()" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="积分算值" prop="num">
						<el-input v-model="form.num" placeholder="请输入积分算值" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="每月上限" prop="monthLimit">
						<el-input v-model="form.monthLimit" placeholder="请输入每月上限" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="每年上限" prop="yearLimit">
						<el-input v-model="form.yearLimit" placeholder="请输入每年上限" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="分值上限" prop="totalLimit">
						<el-input v-model="form.totalLimit" placeholder="请输入分值上限" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="规则状态" prop="status">
						<el-select v-model="form.status" placeholder="请选择规则状态">
							<el-option label="启用" :value="1"></el-option>
							<el-option label="禁用" :value="0"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="生效状态" prop="effectiveStatus">
						<el-select v-model="form.effectiveStatus" placeholder="请选择生效状态">
							<el-option label="开启" :value="1"></el-option>
							<el-option label="关闭" :value="0"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="生效开始时间" prop="effectiveStartTime">
						<el-date-picker type="datetime" v-model="form.effectiveStartTime" placeholder="请输入生效开始时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="生效结束时间" prop="effectiveEndTime">
						<el-date-picker type="datetime" v-model="form.effectiveEndTime" placeholder="请输入生效结束时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="创建时间" prop="createdTime">
						<el-date-picker type="datetime" v-model="form.createdTime" placeholder="请输入创建时间" :disabled="!getEditable()" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="更新时间" prop="updatedTime">
						<el-date-picker type="datetime" v-model="form.updatedTime" placeholder="请输入更新时间" :disabled="!getEditable()" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="BloodxPointRulesDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/blood/bloodxPointRules';
import { rule } from '/@/utils/validate';
import dayjs from 'dayjs';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	ruleName: '',
	sceneKey: '',
	ruleType: '',
	num: '',
	monthLimit: '',
	yearLimit: '',
	totalLimit: '',
	status: '',
	effectiveStatus: '',
	effectiveStartTime: '',
	effectiveEndTime: '',
	createdTime: '',
	updatedTime: '',
});

// 获取可编辑性
function getEditable() {
	const editable = ref(true);
	if (form.id) {
		editable.value = false;
	}
	return editable.value;
}

// 定义校验规则
const dataRules = ref({});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取bloodxPointRules信息
	if (id) {
		form.id = id;
		getbloodxPointRulesData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		if (form.effectiveStartTime) {
			form.effectiveStartTime = dayjs(form.effectiveStartTime).format('YYYY-MM-DD HH:mm:ss');
		}
		if (form.effectiveEndTime) {
			form.effectiveEndTime = dayjs(form.effectiveEndTime).format('YYYY-MM-DD HH:mm:ss');
		}
		if (form.createdTime) {
			form.createdTime = dayjs(form.createdTime).format('YYYY-MM-DD HH:mm:ss');
		}
		if (form.updatedTime) {
			form.updatedTime = dayjs(form.updatedTime).format('YYYY-MM-DD HH:mm:ss');
		}
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getbloodxPointRulesData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
